package com.czk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czk.mapper.CourseMapper;
import com.czk.pojo.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/6/18 16:27
 */
@RestController
public class CourseController {
    @Autowired
    private CourseMapper courseMapper;

    @GetMapping("/course")
    public Course addCourseId() {
        Course course = new Course();
        for (int i = 0; i < 10; i++) {
            course.setCname("物理");
            course.setCstatus("normal");
            course.setUserId(Long.valueOf(i));
            courseMapper.insert(course);
        }
        return course;
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/get/course/{id}")
    public Course selectCourseById(@PathVariable("id") Long id) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", id);
        Course course = courseMapper.selectOne(wrapper);
        return course;
    }

    /**
     * 通过userId和cid演示分库分表操作
     *
     * @param cid
     * @param userId
     * @return
     */
    @GetMapping("/get/course/{cid}/{userId}")
    public Course selectCourseByCidAndUserId(@PathVariable("cid") Long cid,
                                             @PathVariable("userId") Long userId) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", cid);
        wrapper.eq("user_id", userId);
        Course course = courseMapper.selectOne(wrapper);
        return course;
    }

    @GetMapping("/list")
    public List<Course> getCourse() {
        List<Long> list = Arrays.asList(612687216094740480L, 612712082101501953L);

        List<Course> courses=new ArrayList<>();
        for (Long aLong : list) {
            QueryWrapper<Course> wrapper = new QueryWrapper<>();
            wrapper.eq("cid",aLong);
            courses.add(courseMapper.selectOne(wrapper));
        }
        return courses;


    }
}
